<template>
  <y-dialog @open="open" @close="emit('close')" width="1000px" append-to-body>
    <el-form
      ref="addRef"
      :model="form"
      :rules="rules"
      size="default"
      label-width="120px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属产品" prop="product">
            <y-select
              v-model="form.product"
              placeholder="请选择所属产品"
              :optionsFunc="listProduct"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="协议名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入协议名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="协议类别" prop="type">
            <el-input v-model="form.type" placeholder="请输入协议类别" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <y-editor
          editorId="editorProtocolRemark"
          v-model="form.remark"
          placeholder="请输入备注信息"
        />
      </el-form-item>
      <el-row>
        <el-col>
          <el-form-item label="标签" prop="tags">
            <y-tag v-model="form.tags" :closable="true" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
        <el-button @click="emit('close')">取消</el-button>
      </div>
    </template>
  </y-dialog>
</template>

<script setup name="AuProto">
import { listProduct } from "@/api/pms/product";
const { proxy } = getCurrentInstance();

const emit = defineEmits(["submited", "close"]);

const data = reactive({
  form: {},
  queryParams: {
    product: undefined,
    name: undefined,
  },
  rules: {
    product: [{ required: true, message: "所属产品不能为空", trigger: "blur" }],
    name: [{ required: true, message: "协议名称不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

const props = defineProps({
  id: { type: String, default: "" },
  mode: { type: String, default: "X" },
  operate: { type: String, default: "add" },
  parentData: { type: Object, default: {} },
  getMethod: { type: Function, default: undefined },
  addMethod: { type: Function, default: undefined },
  updateMethod: { type: Function, default: undefined },
});

/** 打开对话框事件 */
function open() {
  switch (props.operate) {
    case "update":
      props.getMethod(props.id).then((res) => {
        form.value = res.data;
      });
      break;
    default:
      reset();
      break;
  }
}

/** 重置Form方法 */
function reset() {
  form.value = {
    status: "0",
  };
  proxy.resetForm("addRef");
}

/** 提交按钮事件 */
function submit() {
  proxy.$refs["addRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        props.updateMethod(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功");
          submited();
        });
      } else {
        form.value.parent = props.parentData.id;
        props.addMethod(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功");
          submited();
        });
      }
    }
  });
}

/** submited事件 */
function submited() {
  emit("submited");
  emit("close");
}
</script>
